#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "ALog.h"
#include "search.h"

#define LOG_TAG "kociemba_jni"
#define CACHE_DIR "/storage/emulated/0/Android/data/com.aispeech.aicube/cache"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL
Java_com_aispeech_kociemba_KociembaEngine_nativeProcess(JNIEnv *env, jobject thiz,
                                                        jstring coordinate_info) {
//    char patternized[64];
//    char* facelets = arg[1];
//    char *facelets = (char *) (*env)->GetStringUTFChars(env, coordinate_info, false);

//    if (argc > 2) {
//        patternize(facelets, argv[2], patternized);
//        facelets = patternized;
//    }
//    char *sol = solution(
//            facelets,
//            24,
//            1000,
//            0,
//            CACHE_DIR
//    );

//    (*env)->ReleaseStringUTFChars(env, coordinate_info, facelets);

//    if (sol == NULL) {
////        puts("Unsolvable cube!");
//        return "NULL";
//    }
//    jstring rtstr = (*env)->NewStringUTF(env, sol);

//    puts(sol);
//    free(sol);


//    return rtstr;
    char buf[128];
    char *facelets;
    facelets = (*env)->GetStringUTFChars(env, coordinate_info, NULL);
    if (facelets == NULL)
        return NULL;

    ALOGD("cube: coordinate [%s]", facelets);

    char *sol = solution(
            facelets,
            24,
            1000,
            0,
            CACHE_DIR
    );

    (*env)->ReleaseStringUTFChars(env, coordinate_info, facelets);

    if (!sol) {
        ALOGW("Unsolvable cube!");
        sol = "NULL";
    }

    sprintf(buf, "%s", sol);
    return (*env)->NewStringUTF(env, buf);
}

#ifdef __cplusplus
}
#endif